//
//  RadioButton.m
//  UI02_CustomView
//
//  Created by 蒋 杏飞 on 14-6-24.
//  Copyright (c) 2014年 蒋 杏飞. All rights reserved.
//

#import "RadioButton.h"

@implementation RadioButton

- (id)initWithFrame:(CGRect)frame names:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // 多少列
        int line = self.frame.size.width / 70;
        if (line == 0) {
            return self;
        }
        // 行
        NSInteger row = [array count] / line;
        
        if ([array count] % line > 0) {
            row ++;
        }
        
        for (int i = 0; i < row; i++) {
            
            for (int j = 0; j < line; j ++ ) {
                
                int num = i * line + j;
                if (num > [array count] - 1) {
                    break;
                }
                
                NSString *name = [array objectAtIndex:num];
                
                UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [changeButton setFrame:CGRectMake(j * 100, 0, 60, 40)];
                [changeButton setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
                [changeButton setTitle:name forState:UIControlStateNormal];
                [changeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                changeButton.tag = j+100;
                [changeButton addTarget:self action:@selector(radionAction:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:changeButton];
                
            }
        }
    }
    return self;
}


- (void)radionAction:(UIButton *)sender{
      
    NSLog(@"sender == %@",sender);
    for (UIButton *button in [self subviews]) {
      
        [button setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
    }
    UIButton *aButton = (UIButton *)sender;
    [aButton setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [self.delegate changeSender:sender.tag];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
